//
//  LTMessage.m
//  LTIM
//
//  Created by 俊松何 on 2017/3/27.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTMessage.h"
#import "LTUser.h"
#import "MJExtension.h"

#import "LTDBHelper.h"

#import "LTMessageImageBody.h"
#import "LTMessageTextBody.h"

@implementation LTMessage

+(NSDictionary *)mj_replacedKeyFromPropertyName
{
    return  @{
              @"createTime" : @"date",
              @"serverTime" : @"date",
              };

}

-(void)setServerTime:(long)serverTime
{
    if(_createTime == 0)
    {
        _createTime = serverTime;
    }
    _serverTime = serverTime;
}



-(instancetype)initWithConversationID:(NSString *)conversationID
                           fromUserId:(NSString *)fromUserId
                             toUserId:(NSString *)toUserId
                             fromUser:(LTUser *)fromuser
                               toUser:(LTUser *)toUser
                                 type:(LTMessageType)type
                                 body:(LTMessageBody *)body
                             chatType:(LTChatType)chattype
                                  ext:(NSString *)ext
{
    if(self = [super init]){
        
        LTUser *user = [[LTUser alloc] init];
        user.userId = fromuser.userId;
        user.name = fromuser.name;
        user.avatar = fromuser.avatar;
        user.type = fromuser.type;
        
    _conversationId = conversationID;
    _messageId = [self CreatemessageID];
    _sinceUserId     = fromUserId;
    _targetUserId    = toUserId;
    _body           =  body;
    _direction      = LTMessageDirectionSend;
    _messageType    = type;
    _since          = user;
    _target         = toUser;
    _createTime     = [[NSDate date] timeIntervalSince1970] * 1000;
    _serverTime     = _createTime;
    _chatType       = chattype;
    _isRead         = YES;
    _extra          = ext;
        _level      = 5;
    }
    return self;
}
-(NSString *)CreatemessageID
{
    CFUUIDRef uuidRef =CFUUIDCreate(NULL);
    
    CFStringRef uuidStringRef =CFUUIDCreateString(NULL, uuidRef);
    
    CFRelease(uuidRef);
    
    NSString *uniqueId = (__bridge NSString *)uuidStringRef;
    
    return uniqueId;
}
-(void)markMessageAsRead
{
    self.isRead = YES;
    [[LTDBHelper sharedbhelper] markMessagesAsReadWithMessageId:self.messageId];

}


@end
